package ModelLayer;		//makes this class a part of the ModelLayer package
import java.util.ArrayList;
import java.util.List;
import java.io.*;
/**
 * Write a description of class OrderContainer here.
 * 
 * @author (ucn-1semprojekt-dm78-gruppe1) 
 * 
 */
public class OrderContainer
{
    private ArrayList<SaleOrder> saleorders;
    private ArrayList<RentOrder> rentorders;
    private static OrderContainer instance;
    private int ordersize;

    /**
     * Constructor for objects of class OrderContainer
     */
    public OrderContainer()
    {
        saleorders = new ArrayList<SaleOrder>();
        rentorders = new ArrayList<RentOrder>();
    }
    
    // Singleton 
    public static OrderContainer getInstance()
    {
    	if(instance == null)
    	{
    		instance = new OrderContainer();
    	}
    	return instance;
    }

    /**
     * Add an order to the collection of orders
     * 
     * @param order
     */
    public void addSaleOrder(SaleOrder saleorder)
    {
        saleorders.add(saleorder);
    }
    
    public void addRentOrder(RentOrder rentorder)
    {
        rentorders.add(rentorder);
    }
    
    public void saveToFile() throws IOException
    {
    	FileHandler.gem(saleorders,"SaleOrder.db");
    	FileHandler.gem(rentorders,"RentOrder.db");
    }
    
    @SuppressWarnings("unchecked")
   	public void loadFromFile() throws IOException, ClassNotFoundException
       {
       	try {
           	rentorders = (ArrayList<RentOrder>) FileHandler.hent("RentOrder.db");
           	saleorders = (ArrayList<SaleOrder>) FileHandler.hent("SaleOrder.db");
           } catch (Exception ex)
           {
           	//throw new IOException("Ikke muligt at lave et gyldigt filnavn til");
           }
       }
    
    public SaleOrder getSaleOrder(int ordernr)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        
        while (!found && index < saleorders.size())
        {
            Order order = saleorders.get(index);
            if(order.getOrdernr() == ordernr)
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return saleorders.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    public RentOrder getRentOrder(int ordernr)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        
        while (!found && index < rentorders.size())
        {
            Order order = rentorders.get(index);
            if(order.getOrdernr() == ordernr)
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return rentorders.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    public List<SaleOrder> getOrderList()
    {
    	return this.saleorders;
    }
    
    public int getLastOrder()
    {
    	int end = 0;
    	int indexLastOrder = saleorders.size()-1;
    	if(indexLastOrder < 0)
    	{
    		end = 0;
    	}
    	else
    	{
    		end = saleorders.get(indexLastOrder).getOrdernr();
    	}
    	return end;
    }
    
    public void updateSaleOrder(int ordernr, String date, double totalPrice, Employee employee, int deliveryDiscount, Customer customer, String deliveryDate, double offer)
    {
        SaleOrder saleorder = getSaleOrder(ordernr);
        if(saleorder != null)
        {
            saleorder.setDate(date);
            saleorder.setTotalPrice(totalPrice);
            saleorder.setEmployee(employee);
            saleorder.setDeliveryDiscount(deliveryDiscount);
            saleorder.setDeliveryDate(deliveryDate);
            saleorder.setCustomer(customer);
            saleorder.setOffer(offer);
        }
    }
}